//
//  UserAccount.swift
//  
//
//  Created by 陈锦彭 on 16/3/15.
//
//

import UIKit

class UserAccount: NSObject, NSCoding {
    
    // MARK: - 属性
    var access_token : String?
    var expires_in : NSTimeInterval = 0 {
        didSet {
         overTime = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var uid : String?
    
    // MARK: - 自己定义的属性
    ///授权过期日期
    var overTime : NSDate?
    ///用户头像
    var avatar_large : String?
    ///用户昵称
    var screen_name : String?
    

    
    //遍历构造方法
    init(dict : [String : AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //重写这个方法key和属性对不上就不会报错
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
   
    
    // MARK: - 重写description方法
    //计算属性
    override var description : String {
        get {
          return dictionaryWithValuesForKeys(["access_token", "expires_in", "uid", "overTime", "avatar_large", "screen_name"]).description
        }
    }
    
    // MARK: - 归档协议方法
    ///归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(overTime, forKey: "overTime")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    ///解档
    required init?(coder aDecoder: NSCoder) {
       
        access_token =  aDecoder.decodeObjectForKey("access_token") as? String
        expires_in =  (aDecoder.decodeObjectForKey("expires_in") as? NSTimeInterval)!
        uid =  aDecoder.decodeObjectForKey("uid") as? String
        overTime =  aDecoder.decodeObjectForKey("overTime") as? NSDate
        avatar_large =  aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name =  aDecoder.decodeObjectForKey("screen_name") as? String
    }

    
}



